package com.zb.order.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.order.service.ConWorkSpareService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjWorkSpareLogService;
import com.zb.service.ZjWorkSpareMaterialService;
import com.zb.service.ZjWorkSpareService;
import com.zb.utils.TokenUtil;
import com.zb.vo.WorkSpareCheckVo;
import com.zb.vo.WorkSpareSelectVo;
import com.zb.vo.WorkSpareVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 工单备件表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-10-26
 */
@Api(tags = "工单备件类接口")
@RestController
@RequestMapping("/work/spare")
public class ZjWorkSpareController {
    @Resource
    private ConWorkSpareService conWorkSpareService;
    @Resource
    private ZjWorkSpareService workSpareService;
    @Resource
    private ZjWorkSpareMaterialService workSpareMaterialService;
    @Resource
    private ZjWorkSpareLogService workSpareLogService;

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "服务工单-备件管理/工单详情 - 保存或修改工单备件")
    @ApiOperation("保存或修改工单备件")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid WorkSpareVo workSpareVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conWorkSpareService.save(loginInfo, workSpareVo);
        return Result.success(null == workSpareVo.getId()? "新增申请工单备件成功" : "修改申请工单备件成功");
    }

    @ApiOperation("获取工单备件列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody @Valid WorkSpareSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取工单备件列表成功").data(conWorkSpareService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据工单ID获取工单详情的工单备件列表")
    @PostMapping("/getOrderDetailWorkSpareList/{workId}")
    public Result getOrderDetailWorkSpareList(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取工单详情的工单备件列表成功").data(conWorkSpareService.getOrderDetailWorkSpareList(workId, pageUtil));
    }

    @ApiOperation("根据工单备件ID获取工单备件详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "工单备件ID", required = true) Long id) {
        return Result.success("获取工单备件详情成功").data("row", conWorkSpareService.getDetail(id));
    }

    @ApiOperation("根据工单备件ID获取工单备件详情的物料信息")
    @GetMapping("/getOrderSpareMaterial/{id}")
    public Result getOrderSpareMaterial(@PathVariable @ApiParam(value = "工单备件ID", required = true) Long id) {
        return Result.success("获取工单备件物料成功").data("rows", conWorkSpareService.getOrderSpareMaterial(id));
    }

    @ApiOperation("获取工单备件日志列表")
    @PostMapping("/getOrderSpareLogList/{id}")
    public Result getOrderSpareLogList(@PathVariable @ApiParam(value = "工单备件ID", required = true) Long id,
                                       @RequestBody PageUtil pageUtil) {
        return Result.success("获取工单备件日志列表成功").data(workSpareLogService.getOrderWorkSpareLogList(id, pageUtil));
    }

    @ApiOperation("获取审核前的工单备件信息")
    @GetMapping("/getBeforeCheckOrderSpareDetail/{id}")
    public Result getBeforeCheckOrderSpareDetail(@PathVariable @ApiParam(value = "工单备件ID", required = true) Long id) {
        return Result.success("获取审核前的工单备件信息成功").data("row", workSpareService.getBeforeCheckOrderSpareDetail(id));
    }

    @ApiOperation("审核工单备件")
    @PutMapping("/checkOrderSpare")
    public Result checkOrderSpare(HttpServletRequest request, @RequestBody @Valid WorkSpareCheckVo workSpareCheckVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conWorkSpareService.checkOrderSpare(loginInfo, workSpareCheckVo);
        return Result.success("工单备件审核成功");
    }

}
